<?php

class LightboxgalleryController extends ApplicationController {
	protected $AppName = "lightboxgallery";
	public $UseApps = array(array("name" => "smarty"), array("name" => "mysql"));
	protected $IncludeFiles = array("lbg.album.class.php", "lbg.image.class.php");
	protected $HeadTags = array("<script type=\"text/javascript\" src=\"{\$env.url}other/javascript/lightbox/js/jquery.lightbox-0.5.js\"></script>",
								"<link rel=\"stylesheet\" type=\"text/css\" href=\"{\$env.url}other/javascript/lightbox/css/jquery.lightbox-0.5.css\" />",
								"<link rel=\"stylesheet\" type=\"text/css\" href=\"{\$env.url}lib/lightboxgallery/views/lbgallery.css\" />",
								"<script type=\"text/javascript\" src=\"{\$env.url}lib/lightboxgallery/lbgallery.js\"></script>");
	
	function init(){
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		$HTTP = new HTTPRequest();
		$URL_Action = $HTTP->get($URLPosition);
		
		$SmartyC = $this->UseApps["smarty"];
		$Smarty = $SmartyC->getSmarty();
		
		switch ($URL_Action){
			case "album":
				$AlbumName = $HTTP->get($URLPosition+1);
				if($AlbumName == null){
					return $this->Language->get("not_found");
				}
				
				$GalleryAlbum = new GalleryAlbum($AlbumName, $this);
				$GalleryAlbumImages = $GalleryAlbum->getImages();
				
				$this->ViewFile = "display_album.tpl";
				
				$Smarty->assign("GalleryAlbum", $GalleryAlbum);
				$Smarty->assign("GalleryAlbumImages", $GalleryAlbumImages);
				
				return true;
			break;		
			
			case "image":
				$ImageID = $HTTP->get($URLPosition+1);
				$NewImage = new GalleryImage($ImageID, $this);
				if($NewImage->getID() == null){
					return $this->Language->get("no_image");
				}
				$this->ViewFile = "display_image.tpl";
				
				$Smarty->assign("LbgImage", $NewImage);
				
				return true;
			break;
			
			default:
				$MySQL = $this->UseApps["mysql"];
				
				$AlbumList = $this->getAlbumList();
				
				$AlbumThumbnails = array();
				
				$URL = Core::$FMSConfig->getValue("environment", "url");
				
				foreach ($AlbumList as $Album){
					$AlbumImages = $Album->getImages();
					
					if($AlbumImages === false){
						$AlbumThumbnails[$Album->getAlbumName()] = $URL."content/lbgallery/album_empty.jpg";
					}
					else {
						$CountImages = count($AlbumImages);
						$AlbumThumb = $AlbumImages[$CountImages - 1];
						
						$AlbumThumbnails[$Album->getAlbumName()] = $AlbumThumb->getThumbnailPath();
					}
				}
				
				$this->ViewFile = "display_default.tpl";
				
				$SmartyC = $this->UseApps["smarty"];
				$Smarty = $SmartyC->getSmarty();
				
				$Smarty->assign("AlbumList", $AlbumList);
				$Smarty->assign("AlbumThumbnails", $AlbumThumbnails);
				
				return true;
			break;	
		}
	}
	
	function getAlbumList(){
		$MySQL = $this->UseApps["mysql"];
		
		$AlbumsQuery = $MySQL->query("SELECT album_name FROM {$MySQL->getPrefix()}lightbox_albums ORDER BY id DESC");
		
		$Albums = array();
		
		while($album = mysql_fetch_assoc($AlbumsQuery)){
			$Albums[] = new GalleryAlbum($album["album_name"], $this);
		}
		
		return $Albums;
	}
	
	function getLightboxGallery(){
		return $this;
	}
}

?>